﻿using Aspose.Words;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsposeHelper
{
    public class WordsHelper
    {
        private DocumentBuilder oWordApplic;
        private Aspose.Words.Document oDoc;

        /// <summary>
        /// 导出Word文档 
        /// </summary>
        /// <param name="title">文档标题</param>
        /// <param name="content">文档内容(key小节标题 value小节内容)</param>
        /// <returns></returns>
        public void ExportWords(string savePath, string title, List<WordSection> content)
        {
            if (string.IsNullOrEmpty(savePath))
            {
                throw new Exception("请选择保存位置！");
            }
            else
            {
                string fileName = Path.Combine(savePath, (DateTime.Now.ToString("HHmmssfff") + ".docx"));

                oDoc = new Aspose.Words.Document();
                oWordApplic = new DocumentBuilder(oDoc);

                InsertText(title, 18, true, "center", Color.Black);

                for (int i = 0; i < content.Count; i++)
                {
                    InsertNewline(1);
                    InsertHtml(content[i].title);
                    if (!string.IsNullOrEmpty(content[i].Content))
                    {
                        InsertText(content[i].Content, 9, false, "left", Color.Black);
                    }
                }

                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                oDoc.Save(fileName, SaveFormat.Docx);
            }
        }

        /// <summary>
        /// 添加换行
        /// </summary>
        private void InsertNewline(int count)
        {
            for (int i = 0; i < count; i++)
            {
                oWordApplic.Writeln();
            }
        }

        private void InsertHtml(string content)
        {
            oWordApplic.InsertHtml(content);
        }

        /// <summary> 
        /// 添加内容 
        /// </summary> 
        /// <param name="content"></param> 
        private void InsertText(string content, double conSize, bool conBold, string conAlign, Color fontColor)
        {
            oWordApplic.Bold = conBold;
            oWordApplic.Font.Size = conSize;
            oWordApplic.Font.Color = fontColor;

            switch (conAlign)
            {
                case "left":
                    oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                    break;
                case "center":
                    oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                    break;
                case "right":
                    oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Right;
                    break;
                default:
                    oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                    break;
            }
            oWordApplic.Writeln(content);
        }
    }
}
